/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Contatos.java
 *
 * Created on Nov 11, 2011, 12:37:35 PM
 */
package interfacesBeans;

import javax.swing.JOptionPane;

/**
 *
 * @author Rodrigo
 */
public class Contatos extends javax.swing.JPanel {

    /** Creates new form Contatos */
    public Contatos() {
        initComponents();
        initContactsList();
    }

    public void initContactsList() {
        javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel)tableContatos.getModel();
        model.setNumRows(0);

        for (agenda.Contato con : Body.user.getAgenda().getContatos()) {
            model.addRow(new String[] {con.getNome(), con.getDataAniversarioFormatada()});
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelMain = new javax.swing.JPanel();
        buttonAdicionar = new javax.swing.JButton();
        buttonEditar = new javax.swing.JButton();
        buttonRemover = new javax.swing.JButton();
        buttonVoltar = new javax.swing.JButton();
        labelContatos = new javax.swing.JLabel();
        buttonVisualizarContato = new javax.swing.JButton();
        panelTable = new javax.swing.JPanel();
        scrollPaneTable = new javax.swing.JScrollPane();
        tableContatos = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));

        panelMain.setBackground(new java.awt.Color(255, 255, 255));

        buttonAdicionar.setBackground(new java.awt.Color(255, 255, 255));
        buttonAdicionar.setText("Adicionar Contato");
        buttonAdicionar.setPreferredSize(new java.awt.Dimension(130, 50));
        buttonAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAdicionarActionPerformed(evt);
            }
        });

        buttonEditar.setBackground(new java.awt.Color(255, 255, 255));
        buttonEditar.setText("Editar Contato");
        buttonEditar.setPreferredSize(new java.awt.Dimension(130, 50));
        buttonEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditarActionPerformed(evt);
            }
        });

        buttonRemover.setBackground(new java.awt.Color(255, 255, 255));
        buttonRemover.setText("Remover Contato");
        buttonRemover.setPreferredSize(new java.awt.Dimension(130, 50));
        buttonRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRemoverActionPerformed(evt);
            }
        });

        buttonVoltar.setBackground(new java.awt.Color(255, 255, 255));
        buttonVoltar.setText("Voltar");
        buttonVoltar.setPreferredSize(new java.awt.Dimension(125, 50));
        buttonVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVoltarActionPerformed(evt);
            }
        });

        labelContatos.setBackground(new java.awt.Color(255, 255, 255));
        labelContatos.setFont(new java.awt.Font("Tahoma", 1, 28));
        labelContatos.setText("Contatos");

        buttonVisualizarContato.setBackground(new java.awt.Color(255, 255, 255));
        buttonVisualizarContato.setText("Visualizar Contato");
        buttonVisualizarContato.setPreferredSize(new java.awt.Dimension(130, 50));
        buttonVisualizarContato.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVisualizarContatoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelMainLayout = new javax.swing.GroupLayout(panelMain);
        panelMain.setLayout(panelMainLayout);
        panelMainLayout.setHorizontalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelMainLayout.createSequentialGroup()
                        .addComponent(buttonAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonVisualizarContato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonEditar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonRemover, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(buttonVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(labelContatos))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        panelMainLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonAdicionar, buttonEditar, buttonRemover, buttonVoltar});

        panelMainLayout.setVerticalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelContatos)
                .addGap(18, 18, 18)
                .addGroup(panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonAdicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonVisualizarContato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonEditar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonRemover, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        panelMainLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {buttonAdicionar, buttonEditar, buttonRemover, buttonVoltar});

        panelTable.setBackground(new java.awt.Color(255, 255, 255));

        scrollPaneTable.setBackground(new java.awt.Color(255, 255, 255));

        tableContatos.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tableContatos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Data de Aniversário"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrollPaneTable.setViewportView(tableContatos);

        javax.swing.GroupLayout panelTableLayout = new javax.swing.GroupLayout(panelTable);
        panelTable.setLayout(panelTableLayout);
        panelTableLayout.setHorizontalGroup(
            panelTableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPaneTable, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        panelTableLayout.setVerticalGroup(
            panelTableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPaneTable, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelTable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panelMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelTable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonVisualizarContatoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVisualizarContatoActionPerformed
        if (tableContatos.getSelectedRow() != -1) {
            Body.visualizarContato.setInitialFieldValues(tableContatos.getSelectedRow());
            java.awt.CardLayout layout = (java.awt.CardLayout)Body.panelBody.getLayout();
            layout.show(Body.panelBody, "VisualizarContato");
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um contato!", "Ação inválida", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_buttonVisualizarContatoActionPerformed

    private void buttonAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAdicionarActionPerformed
        Body.adicionarContato.setInitialFieldValues();
        Body.adicionarContato.setFieldsEnderecoFormaDecontato();
        java.awt.CardLayout layout = (java.awt.CardLayout)Body.panelBody.getLayout();
        layout.show(Body.panelBody, "AdicionarContato");
    }//GEN-LAST:event_buttonAdicionarActionPerformed

    private void buttonVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVoltarActionPerformed
        java.awt.CardLayout layout = (java.awt.CardLayout)Body.panelBody.getLayout();
        layout.show(Body.panelBody, "TelaInicial");
    }//GEN-LAST:event_buttonVoltarActionPerformed

    private void buttonRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRemoverActionPerformed
        javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel)tableContatos.getModel();
        int selectedRow = tableContatos.getSelectedRow();
        if (selectedRow != -1) {
            int option = JOptionPane.showConfirmDialog(this, "Tem certeza que deseja remover este contato?", "Remoção de contato", JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                model.removeRow(selectedRow);
                Body.user.getAgenda().getContatos().remove(selectedRow);
                JOptionPane.showMessageDialog(this, "Contato removido com sucesso!", "Contato removido", JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um contato!", "Ação inválida", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_buttonRemoverActionPerformed

    private void buttonEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditarActionPerformed
        if (tableContatos.getSelectedRow() != -1) {
            Body.editarContato.setInitialFieldValues(tableContatos.getSelectedRow());
            java.awt.CardLayout layout = (java.awt.CardLayout)Body.panelBody.getLayout();
            layout.show(Body.panelBody, "EditarContato");
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um contato!", "Ação inválida", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_buttonEditarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAdicionar;
    private javax.swing.JButton buttonEditar;
    private javax.swing.JButton buttonRemover;
    private javax.swing.JButton buttonVisualizarContato;
    private javax.swing.JButton buttonVoltar;
    private javax.swing.JLabel labelContatos;
    private javax.swing.JPanel panelMain;
    private javax.swing.JPanel panelTable;
    private javax.swing.JScrollPane scrollPaneTable;
    public static javax.swing.JTable tableContatos;
    // End of variables declaration//GEN-END:variables
}
